< previous page page_47 next page >

Page 47
Puzzle 10
Environmentally Speaking
Every now and then, you'll run into a DLL function that returns a string whose return type is LPSTR or LPTSTR. You won't find too many of these in the core Win32 API, but some custom DLLs and extension libraries take this approach. Consider one example from the Win32 APIthe GetEnvironmentStrings function. Computer programs, like all living things, live in the context of an environment. The GetEnvironmentStrings function lets you examine the environment variables for an application. This function is described in the C documentation as follows:
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
Each process has an environment block associated with it. The environment block consists of a null-terminated block of null-terminated strings (meaning there are two null bytes at the end of the block), where each string is in the following form:
7017a6ead0e3c4111b47a554df321e9f.gif
name=value
7017a6ead0e3c4111b47a554df321e9f.gif
LPVOID GetEnvironmentStrings(VOID)
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
When GetEnvironmentStrings is called, it allocates memory for a block of environment strings. When the block is no longer needed, it should be freed by calling FreeEnvironmentStrings.
Even though the declaration is LPVOID, from the explanation you can see that it is actually returning a pointer to a string. If you look at the declaration in the Win32api.txt file that comes with Visual Basic, you'll find the function declared as follows:
Declare Function GetEnvironmentStrings Lib "kernel32" Alias
"GetEnvironmentStringsA" () As String
The EnvStr.vbp project (on the CD that comes with this book) uses this function to obtain the environment strings for an application as follows:

 
< previous page page_47 next page >